package com.docMan.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.docMan.service.DocAdminService;
import com.docMan.service.TchumanService;
import com.docMan.utils.FinalConfig;


public class UserAction extends BaseAction implements SessionAware,ServletRequestAware {
	private HttpServletRequest servletRequest;
	private TchumanService tchumanService;
	private String message;
	private String username;
	private String password;
	private String redirectUrl;
	private DocAdminService docAdminService;
	
	@Autowired
	public void setDocAdminService(DocAdminService docAdminService) {
		this.docAdminService = docAdminService;
	}
	public String getRedirectUrl() {
		return redirectUrl;
	}
	public void setRedirectUrl(String redirectUrl) {
		this.redirectUrl = redirectUrl;
	}
	@Resource(name="tchumanService")
	public void setTchumanService(TchumanService tchumanService) {
		this.tchumanService = tchumanService;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest=request;
	}
	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}
//	@Action(value="loginView",results={@Result(name="success",location="/login.jsp")})
	public String loginView(){
		return "success";
	}
	
//	@Action(value="login",results={@Result(name="success",location="${redirectUrl}",type="redirect")})
	public String login(){
		redirectUrl=(String) session.get(FinalConfig.REDIRECT_URL);
		try {
			int loginResult=tchumanService.login(username, password);
			if(loginResult==FinalConfig.LOGIN_NO_USER){
				message="用户名不存在";
				return "login";
			}else if (loginResult==FinalConfig.LOGIN_PASSWORD_WRONG) {
				message="用户名或密码错误";
				return "login";
			}
			boolean isRedirect=(boolean) session.get(FinalConfig.IS_REDIRECT);
			if((boolean) session.get(FinalConfig.IS_REDIRECT)){
				return "success";
			}
			return "index"; 
		} catch (Exception e) {
			e.printStackTrace();return "login";
		}
	}
	
//	@Action(value="logout",results={@Result(name="success",location="loginView",type="redirectAction")})
	public String logout(){
		session.clear();
		ServletActionContext.getRequest().getSession().invalidate();
		boolean isValid=ServletActionContext.getRequest().isRequestedSessionIdValid();
		return "success";
	}
	
}
